﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.Design
Imports System.Globalization
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Serialization
Imports System.Xml

Namespace System.Workflow.ComponentModel.Design
    Public Class ConnectorLayoutSerializer
        Inherits WorkflowMarkupSerializer
        ' Methods
        Protected Overrides Function CreateInstance(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal type As Type) As Object
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (type Is Nothing) Then
                Throw New ArgumentNullException("type")
            End If
            Dim connector As Connector = Nothing
            Dim service As IReferenceService = [TryCast](serializationManager.GetService(GetType(IReferenceService)),IReferenceService)
            Dim designer As FreeformActivityDesigner = [TryCast](serializationManager.Context.Item(GetType(FreeformActivityDesigner)),FreeformActivityDesigner)
            If ((Not designer Is Nothing) AndAlso (Not service Is Nothing)) Then
                Dim source As ConnectionPoint = Nothing
                Dim target As ConnectionPoint = Nothing
                Try 
                    Dim connectorConstructionArguments As Dictionary([Of] String, String) = Me.GetConnectorConstructionArguments(serializationManager, type)
                    If ((connectorConstructionArguments.ContainsKey("SourceActivity") AndAlso connectorConstructionArguments.ContainsKey("SourceConnectionIndex")) AndAlso connectorConstructionArguments.ContainsKey("SourceConnectionEdge")) Then
                        Dim associatedDesigner As ActivityDesigner = ActivityDesigner.GetDesigner([TryCast](service.GetReference(connectorConstructionArguments.Item("SourceActivity")),Activity))
                        Dim designerEdge As DesignerEdges = DirectCast(Enum.Parse(GetType(DesignerEdges), connectorConstructionArguments.Item("SourceConnectionEdge")), DesignerEdges)
                        Dim connectionIndex As Integer = Convert.ToInt32(connectorConstructionArguments.Item("SourceConnectionIndex"), CultureInfo.InvariantCulture)
                        If (((Not associatedDesigner Is Nothing) AndAlso (designerEdge <> DesignerEdges.None)) AndAlso (connectionIndex >= 0)) Then
                            source = New ConnectionPoint(associatedDesigner, designerEdge, connectionIndex)
                        End If
                    End If
                    If ((connectorConstructionArguments.ContainsKey("TargetActivity") AndAlso connectorConstructionArguments.ContainsKey("TargetConnectionIndex")) AndAlso connectorConstructionArguments.ContainsKey("TargetConnectionEdge")) Then
                        Dim designer3 As ActivityDesigner = ActivityDesigner.GetDesigner([TryCast](service.GetReference(connectorConstructionArguments.Item("TargetActivity")),Activity))
                        Dim edges2 As DesignerEdges = DirectCast(Enum.Parse(GetType(DesignerEdges), connectorConstructionArguments.Item("TargetConnectionEdge")), DesignerEdges)
                        Dim num2 As Integer = Convert.ToInt32(connectorConstructionArguments.Item("TargetConnectionIndex"), CultureInfo.InvariantCulture)
                        If (((Not designer3 Is Nothing) AndAlso (edges2 <> DesignerEdges.None)) AndAlso (num2 >= 0)) Then
                            target = New ConnectionPoint(designer3, edges2, num2)
                        End If
                    End If
                Catch obj1 As Object
                End Try
                If ((Not source Is Nothing) AndAlso (Not target Is Nothing)) Then
                    connector = designer.AddConnector(source, target)
                End If
            End If
            Return connector
        End Function

        Protected Function GetConnectorConstructionArguments(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal type As Type) As Dictionary([Of] String, String)
            Dim dictionary As New Dictionary([Of] String, String)
            Dim reader As XmlReader = [TryCast](serializationManager.WorkflowMarkupStack.Item(GetType(XmlReader)),XmlReader)
            If ((Not reader Is Nothing) AndAlso (reader.NodeType = XmlNodeType.Element)) Then
                Do While reader.MoveToNextAttribute
                    Dim localName As String = reader.LocalName
                    If Not dictionary.ContainsKey(localName) Then
                        reader.ReadAttributeValue
                        dictionary.Add(localName, reader.Value)
                    End If
                Loop
                reader.MoveToElement
            End If
            Return dictionary
        End Function

        Protected Friend Overrides Function GetProperties(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As PropertyInfo()
            If (serializationManager Is Nothing) Then
                Throw New ArgumentNullException("serializationManager")
            End If
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim list As New List([Of] PropertyInfo)(MyBase.GetProperties(serializationManager, obj)) { GetType(Connector).GetProperty("SourceActivity", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(Connector).GetProperty("SourceConnectionIndex", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(Connector).GetProperty("SourceConnectionEdge", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(Connector).GetProperty("TargetActivity", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(Connector).GetProperty("TargetConnectionIndex", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(Connector).GetProperty("TargetConnectionEdge", (BindingFlags.NonPublic Or BindingFlags.Instance)), GetType(Connector).GetProperty("Segments", (BindingFlags.NonPublic Or BindingFlags.Instance)) }
            Return list.ToArray
        End Function

        Protected Overrides Sub OnAfterDeserialize(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
            MyBase.OnAfterDeserialize(serializationManager, obj)
            Dim connector As Connector = [TryCast](obj,Connector)
            If (Not connector Is Nothing) Then
                connector.SetConnectorModified(True)
            End If
        End Sub

    End Class
End Namespace

